#include "StdAfx.h"
#include "hash_calculator.h"

hash_calculator::hash_calculator(void)
{
}

hash_calculator::~hash_calculator(void)
{
}

void 
hash_calculator::LoopProc(CString * pMsgPack, PWORKER_PARAMETER work_para)
{
	char ptr_key[64] = {0};
	this->md5_.MD5_File((LPCTSTR)(*pMsgPack),  (unsigned char*)ptr_key);

	{/// make scope
		_UTIL::CScopedLock(this->ret_cont_lock_);

		this->ret_cont_[(*pMsgPack)] = ptr_key;
	}

}

CString 
hash_calculator::get_file_hash(LPCTSTR ptr_fn)
{
	{/// make scope
		_UTIL::CScopedLock(this->ret_cont_lock_);

		ret_cont::iterator pos = this->ret_cont_.find(ptr_fn);
		if ( pos != this->ret_cont_.end() )
		{
			return pos->second;
		}
	}
	return _T("");
}

void 
hash_calculator::push_message(LPCTSTR ptr_fn)
{
	CString* ptr_msg = new CString;/// handled del
	*ptr_msg = ptr_fn;
	Add(ptr_msg);
}

void 
hash_calculator::OnBeforeExiting()
{
	_UTIL::CMsgQueueExThreads<CString>::OnBeforeExiting();
	this->md5_.cancel_current();
}

void 
hash_calculator::set_file_hash(LPCTSTR ptr_fn, LPCTSTR ptr_hash)
{
	_UTIL::CScopedLock(this->ret_cont_lock_);

	this->ret_cont_[ptr_fn] = ptr_hash;
}
